package com.weasel.http.builder;

import com.weasel.helper.JsonHelper;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.StringEntity;

import java.util.Map;

/**
 * Created by Dylan on 2015/10/8.
 */
public abstract class AbstractHttpEntityMethodBaseBuilder<M extends AbstractHttpEntityMethodBaseBuilder> extends AbstractHttpMethodBuilder<M>{


    public M withEntity(HttpEntity entity){

        ((HttpEntityEnclosingRequestBase)getMethod()).setEntity(entity);
        return (M)this;
    }

    /**
     * 将Map转成json作为params
     * @param params
     * @return
     */
    public M withParams(Map<String,Object> params){

        withParam(params);
        return (M)this;
    }

    /**
     * 将Object转成json作为params
     * @param object
     * @return
     */
    public M withParam(Object object){

        withEntity(new StringEntity(JsonHelper.toJsonString(object), Consts.UTF_8));
        return (M)this;
    }
}
